package com.webService;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * InetAddress类
 * Created by IntelliJ IDEA.
 * User: zzx
 * Date: 2017/11/12
 * Time: 下午6:09
 */

public class MyInetAddress {
    public static void main(String[] args) throws UnknownHostException {

        //获取本机的InetAddress实例
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address);

        String hostName = address.getHostName();//获取计算机名
        String ipStr = address.getHostAddress();//获取IP地址

        System.out.println(hostName);
        System.out.println(ipStr);

        byte[] bytes = address.getAddress();//获取字节数组形式的IP地址,以点分隔的四部分
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(bytes[i]);
            System.out.println(bytes[i]);
        }
        String str = sb.toString();
        System.out.println(str);


        //获取其他主机的InetAddress实例
        InetAddress address2 = InetAddress.getByName("www.baidu.com");
        InetAddress address3 = InetAddress.getByName("115.239.211.112");
        System.out.println(address2);
        System.out.println(address3);

    }
}
